<template>
  <div class="top-box">
    <div style="cursor: pointer" class="fs-3" @click="router.push('/message')">&lt;</div>
    <div class="fs-3" v-if="message.mid">{{ message.title }}</div>
    <div>
      <HuangUserInfo></HuangUserInfo>
    </div>
  </div>

  <div class="message">
    <div v-if="message.mid">
      {{ message.user.info.nickname }}
      {{ Tools.formatDate(message.lastupdate, 'yyyy-MM-dd hh:mm:ss') }}
    </div>
    <div v-else>
      <NEmpty description="留言板获取中。。。"></NEmpty>
    </div>
    <div v-html="message.info"></div>
    <div>浏览量：{{ message.hits }} 点赞数：{{ message.praise }}</div>
    <div @click="likeMessage(d)" :class="{ active: d.exInfo.praised }">赞</div>
  </div>

  <NDivider>评论</NDivider>
  <div v-if="list.length == 0">
    <NEmpty description="暂无评论"></NEmpty>
    <div class="tc">
      <NButton @click="showAdd" type="primary">抢先发布</NButton>
    </div>
  </div>

  <div v-else>
    <div class="tc p-2">
      <NButton @click="showAdd" type="primary">发布评论</NButton>
    </div>
    <div class="replies">
      <div v-for="d in list" :key="d.mid">
        <div class="content">
          <div>
            <NAvatar round :src="Api.userlogo(d.mid)"></NAvatar>
          </div>
          <div>
            <div>
              {{ d.user.info.nickname || d.user.user.username }}
              点击量:{{ message.hits }} 评论数：{{ message.reply }}
              <div class="you">
                -<span @click="praiseReply(d)" :class="{ active: d.exInfo.praised }">赞</span>
                <span>({{ d.praise }})</span>
              </div>
              <div>
                {{ d.info }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 发布评论的对话框 -->
  <div>
    <NModal preset="dialog" v-model:show="visible" title="发布评论">
      <div>
        <NInput type="textarea" v-model:value="info" placeholder="评论内容"></NInput>
      </div>
      <template #action>
        <NButton type="primary" @click="addReply">发布</NButton>
      </template>
    </NModal>
  </div>
</template>

<script setup lang="ts">
import { Api } from '@/types/Api'
import type { BaseDataResult, BaseResult, PageBean, PageVo } from '@/types/BaseResult'
import type { TbMessage, TbMessageReply } from '@/types/TbMessage'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { NAvatar, NButton, NEmpty, NInput, NModal, useMessage, NDivider } from 'naive-ui'
import { Tools } from '@/types/Tools'
import HuangUserInfo from '@/components/HuangUserInfo.vue'
const messages = useMessage()

const router = useRouter()
const route = useRoute()

const queryInfo = ref({ order: 1, info: '' })

const mid = route.params.mid
console.log('留言板id:', mid)

const loading = ref(false)

//#region 留言详情
const message = ref({} as TbMessage)
const query = () => {
  loading.value = true
  Api.get(`/message/${mid}`, {}, (data: BaseDataResult<TbMessage>) => {
    loading.value = false
    if (data.success) {
      message.value = data.data
      queryReply()
      return
    }
    messages.error(data.message)
  })
}
//#endregion

//#region 留言评论
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as Array<TbMessageReply>)

const queryReply = () => {
  loading.value = true
  Api.get(
    `/message/reply/${mid}/${page.value.pageSize}/${page.value.pageNumber}/${queryInfo.value.order}`,
    {},
    (data: BaseDataResult<PageVo<TbMessageReply>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      messages.error(data.message)
    },
  )
}
//留言点赞
const praiseReply = (info: TbMessageReply) => {
  Api.post(`/reply/praise/${info.mrid}`, {}, (data: BaseDataResult<number>) => {
    if (data.success) {
      info.praise = data.data
      info.exInfo.praised = !info.exInfo.praised
      return
    }
    messages.error(data.message)
  })
}

//#endregion

//#region 评论点赞
const likeMessage = (mid: TbMessage) => {
  Api.post(`/message/praise/${mid.mid}`, {}, (data: BaseDataResult<number>) => {
    if (data.success) {
      mid.exInfo.praised = !mid.exInfo.praised
      return
    }
    messages.error(data.message)
  })
}

//#endregion

//#region 发布留言评论
const visible = ref(false)
const info = ref('')

const showAdd = () => {
  info.value = ''
  visible.value = true
}

const addReply = () => {
  loading.value = true
  Api.post(
    '/reply',
    {
      mid: mid,
      info: info.value,
    },
    (data: BaseResult) => {
      loading.value = false
      if (data.success) {
        visible.value = false
        messages.success(data.message)
      } else {
        messages.error(data.message)
      }
    },
  )
}

//#endregion

onMounted(() => {
  query()
})
</script>

<style scoped lang="scss">
.top-box {
  display: flex;
  padding: 1rem;
  justify-content: space-between;
}

.message {
  width: 50vw;
  margin: 0px auto;
  :deep(p) {
    font-size: 1.2rem;
    text-indent: 2em;
  }

  > div:first-of-type {
    font-size: 1.2rem;
  }
}

.replies {
  width: 50vw;
  margin: 0px auto;
  > div {
    margin-bottom: 0.5rem;
  }
  .content {
    display: flex;
    align-items: flex-start;
    > div:last-of-type {
      flex: 1;
      margin-left: 0.5rem;
    }
    .active {
      color: #ff0000;
    }
  }
}
.you {
  text-align: right;
}

.detail {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .actives {
    color: #ff0000;
  }
}
</style>
